<template>
  <div class="upload-container">
    <el-upload
      :data="dataObj"
      multiple
      :headers="headers"
      :show-file-list="false"
      :on-success="handleSuccess"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :before-upload="beforeUpload"
      accept="image/jpeg, image/gif, image/png, image/bmp"
      class="image-uploader"
      action="http://amc.yydz.com/attachs/upload"
    >
      <el-button
        size="small"
        type="primary"
      >点击上传</el-button>
      <div
        slot="tip"
        class="el-upload__tip"
      >只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>
    <!-- 拖拽排序 -->
    <draggable
      v-model="fileData"
      tag="el-row"
      v-bind="dragOptions"
      class="row-bg"
      @start="drag = true"
      @end="drag = false"
    >
      <el-col
        v-for="(item,index) in fileData"
        :key="index"
        v-loading="loadingapp"
        :span="6"
      >
        <div class="image-preview">
          <el-image
            v-if="item.url"
            style="width: 100%; height: 150px;"
            :src="item.url"
            fit="cover"
          />
          <div class="image-preview-action">
            <span @click="handlePreview(item,index)">
              <i class="el-icon-zoom-in" />
            </span>
            <span @click="handleDownload(index)">
              <i class="el-icon-download" />
            </span>
            <span @click="handleRemove(file)">
              <i class="el-icon-delete" />
            </span>
          </div>
          <el-input
            v-model="fileData[index].description"
            type="textarea"
            rows="3"
            resize="none"
            @change="handleMark"
          />
        </div>
      </el-col>
    </draggable>
    <!-- 预览弹窗 -->
    <el-dialog :visible.sync="dialogVisible">
      <img
        width="100%"
        :src="dialogImageUrl"
        alt
      >
    </el-dialog>
  </div>
</template>

<script>
import { getAccessToken } from '@/utils/auth'
import draggable from 'vuedraggable'

export default {
  name: 'Gallery',
  components: {
    draggable
  },
  props: {
    fileList: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      loadingapp: false,
      allList: [],
      fileData: [],
      tempUrl: '',
      dataObj: {
        scope: 'default'
      },
      headers: {
        Authorization: ''
      },
      drag: false
    }
  },
  computed: {
    dragOptions() {
      return {
        animation: 200,
        group: 'description',
        disabled: false,
        ghostClass: 'ghost'
      }
    }
  },
  watch: {
    fileData: {
      handler(newValue, oldValue) {
        this.$emit('change', newValue)
      },
      deep: true
    }
  },
  mounted() {
    this.fileData = this.fileList
  },
  methods: {
    beforeUpload() {
      const _self = this
      return new Promise((resolve, reject) => {
        console.log(_self._data.headers)
        _self._data.headers.Authorization = 'Bearer ' + getAccessToken()
        resolve(true)
      })
    },
    handleRemove(index) {
      console.log(index)
      this.fileData.splice(index, 1)
      console.log(this.fileData)
    },
    handlePreview(file, index) {
      console.log(file)
      console.log(index)
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleSuccess(res, file) {
      const { data } = res
      var temp = {
        url: data.url,
        description: file.name
      }
      this.fileData.push(temp)
    },
    handleMark(val) {
      console.log(val)
      console.log(this.fileData)
    },
    handleDownload(file) {
      console.log(file)
    }
  }
}
</script>

<style lang="scss" scoped>
.upload-container {
  width: 100%;
  position: relative;

  .el-row {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-col {
    width: 200px;
    border-radius: 4px;
    margin-right: 20px;
    margin-bottom: 20px;
  }
  >>> .el-textarea__inner {
    border: none;
    border-top-left-radius: 0;
    border-top-right-radius: 0;
  }

  .el-image {
    border-radius: 4px 4px 0 0;
    display: block;
  }
  .row-bg {
    padding: 10px 0;
  }

  .image-preview {
    position: relative;
    border: 1px dashed #d9d9d9;
    width: 200px;
    border-radius: 4px;
    min-height: 36px;

    .image-preview-action {
      position: absolute;
      width: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      transition: opacity 0.3s;
      cursor: pointer;
      text-align: center;
    }
    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }
}
</style>
